#include "exportfile.h"
#include "ui_exportfile.h"
#include "utils.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include<QFileDialog>
using namespace utils;

exportfile::exportfile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::exportfile)
{
    ui->setupUi(this);
    ui->pushButton_3->hide();
    ui->pushButton_3->setEnabled(false);
}

exportfile::~exportfile()
{
    delete ui;
}
//导出学生
void exportfile::on_pushButton_clicked()
{
    int row = exportstulist.size(); //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(3); //设置列数
    QStringList header;
    header << "学工号" << "姓名" << "专业";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数

    for(int i = 0; i < row; i++) {
        Student* tempStu = studentstore::creatStudent();
        tempStu=exportstulist[i];

        QString id = QString(QString::fromLocal8Bit(tempStu->stu_id, MAX_ID));
        QString name = QString(QString::fromLocal8Bit(tempStu->name, MAX_NAME));
        QString major = QString(QString::fromLocal8Bit(tempStu->major, MAX_MAJOR));
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(id));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(name));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(major));

        delete(tempStu);
    }



    ofstream outFile;
    //outFile.open("C:\\Users\\dell\\Documents\\2021_news\\soft\\Library1.4\\stuList_output.csv", ios::out); //打开模式可省略
    QString fileName(tr("exportstu.csv")) ;
     QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                   QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
        dir=dir+"/"+fileName;
//        QDir d;
//        d.mkpath(dir);//可以不用，因为路径已经有了，就不用mk了
        outFile.open(Qstr2str(dir));

    for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
        QString str = ui->tableWidget->item(i, 0)->text();
        string name = string((const char *)str.toLocal8Bit());
        QString str_2 = ui->tableWidget->item(i, 1)->text();
        string stu_id = string((const char *)str_2.toLocal8Bit());
        QString str_3 = ui->tableWidget->item(i, 2)->text();
        string major = string((const char *)str_3.toLocal8Bit());
        outFile << name << "," << stu_id << "," << major << endl;
    }
    outFile.close();

}
//导出书目
void exportfile::on_pushButton_2_clicked()
{
    int row = exportmenulist.size(); //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(9); //设置列数
    QStringList header;
    header << "ID" << "ISBN" << "书名" << "作者" << "borrowCount" << "countNum" << "remain" << "reserveNum" << "exist";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数

    for(int i = 0; i < row; i++) {
        Menu* tempMenu = bookstore::createMenu();
        tempMenu =exportmenulist[i];

        QString ID = QString::number(tempMenu->ID);
        QString ISBN = QString(QString::fromLocal8Bit(tempMenu->ISBN, MAX_ISBN));
        QString name = QString(QString::fromLocal8Bit(tempMenu->name, MAX_BOOK_NAME));
        QString writer = QString(QString::fromLocal8Bit(tempMenu->writer, MAX_WRITER));
        QString borrowCount = QString::number(tempMenu->borrowCount);
        QString countNum = QString::number(tempMenu->countNum);
        QString remain = QString::number(tempMenu->remain);
        QString reserveNum = QString::number(tempMenu->reserveNum);
        QString exist = QString::number(tempMenu->exist);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(ID));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(ISBN));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(name));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(writer));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(borrowCount));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(countNum));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(remain));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(reserveNum));
        ui->tableWidget->setItem(i, 8, new QTableWidgetItem(exist));

        delete(tempMenu);
    }

    ofstream outFile;
    //outFile.open("C:\\Users\\dell\\Documents\\2021_news\\soft\\Library1.4\\stuList_output.csv", ios::out); //打开模式可省略
    QString fileName(tr("exportbook.csv")) ;
     QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                   QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
        dir=dir+"/"+fileName;
//        QDir d;
//        d.mkpath(dir);//可以不用，因为路径已经有了，就不用mk了
        outFile.open(Qstr2str(dir));
        for (int i = 0; i < ui->tableWidget->rowCount(); i++) {

            QString str = ui->tableWidget->item(i, 0)->text();
            string id = string((const char *)str.toLocal8Bit());
            QString str_2 = ui->tableWidget->item(i, 1)->text();
            string ISBN = string((const char *)str_2.toLocal8Bit());
            QString str_3 = ui->tableWidget->item(i, 2)->text();
            string name = string((const char *)str_3.toLocal8Bit());
            QString str_4 = ui->tableWidget->item(i, 3)->text();
            string writer = string((const char *)str_4.toLocal8Bit());
            QString str_5 = ui->tableWidget->item(i, 4)->text();
            string borrow_count = string((const char *)str_5.toLocal8Bit());
            QString str_6 = ui->tableWidget->item(i,5)->text();
            string remain = string((const char *)str_6.toLocal8Bit());
            QString str_7 = ui->tableWidget->item(i,6)->text();
            string renum = string((const char *)str_7.toLocal8Bit());
            QString str_8 = ui->tableWidget->item(i,7)->text();
            string exist = string((const char *)str_8.toLocal8Bit());
            outFile << id << "," << ISBN << "," << name <<"," << writer<< ","<< borrow_count<<","<<remain <<","<< renum<<","<<exist<<""<<endl;
        }
        outFile.close();


}
//导出记录
void exportfile::on_pushButton_3_clicked()
{

    int row = exportrecordlist.size(); //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(8); //设置列数
    QStringList header;
    header << "ID" << "userID" << "bookID" << "borrowTime" << "latestReturnTime" << "returnTime" << "status" << "isReborrow";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数

    ofstream outFile;
    QString fileName(tr("exportrecord.csv")) ;
     QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                   QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
        dir=dir+"/"+fileName;
//        QDir d;
//        d.mkpath(dir);//可以不用，因为路径已经有了，就不用mk了
        outFile.open(Qstr2str(dir));



    for(int i = 0; i < row; i++) {
        Record* tempRecord = bookstore::createRecord();
        tempRecord=exportrecordlist[i];

        QString ID = QString::number(tempRecord->ID);
        QString userId = QString::number(tempRecord->userId);
        QString bookId = QString::number(tempRecord->bookId);
//        QString borrowTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->borrowTime), 24));
//        QString latestReturnTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->latestReturnTime), 24));
//        QString returnTime = QString(QString::fromLocal8Bit(ctime(&tempRecord->returnTime), 24));
        QString borrowTime = fomatTime(tempRecord->borrowTime);
        QString latestReturnTime = fomatTime(tempRecord->latestReturnTime);
        QString returnTime = fomatTime(tempRecord->returnTime);
        QString status = QString::number(tempRecord->status);
        QString isReborrwed = QString::number(tempRecord->isReborrwed);

        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(ID));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(userId));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(bookId));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(borrowTime));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(latestReturnTime));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(returnTime));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(status));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(isReborrwed));

        delete(tempRecord);
    }

}
